package com.ultrapower.ctool.view;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.bean.SearchTestBean;
import com.ultrapower.ctool.bean.WangGeVO;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.model.RegisterRolePirVO;
import com.ultrapower.ctool.xn_app.R;
import com.ultrapower.ctool.xn_app.WangGeActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ctool on 2017/5/17.
 */

public class WgXXPopWindow extends PopupWindow {
    private View contentView;//页面
    private Context context;//上下文
    private View noView;

    //private WangGeVO wgVO = new WangGeVO();
    //;//参数
    private String wgName;

    private EditText wgxx_name;
    private static int DEFAULT_HINT_SIZE = 5;//默认提示框显示项的个数
    private static int hintSize = DEFAULT_HINT_SIZE;//提示框显示项的个数
    //private OkHttpClient client = new OkHttpClient();
    private Gson gson = new Gson();
    Map<String, SearchTestBean> dbMap = new HashMap<String, SearchTestBean>();
    private CustomApplication application;
    private TextView filterReset;//重置
    private TextView filterSure;//确定

    public static void setHintSize(int hintSize) {//设置提示框显示项的个数
        WgXXPopWindow.hintSize = hintSize;
    }

    public WgXXPopWindow(Activity context, final Handler handler, CustomApplication application) {
        this.application = application;
        this.context = context;
        initViews(handler);
    }

    /**
     * 初始化视图
     */
    private void initViews(final Handler handler) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//加载布局
        contentView = inflater.inflate(R.layout.pop_wgxx_details, null);//弹框界面
        wgxx_name = (EditText) contentView.findViewById(R.id.wgxx_name);
        //searchView = (SearchView) contentView.findViewById(R.id.wgxx_search_grid);
        //设置监听
         //searchView.setSearchViewListener(this);
        //设置adapter
//        searchView.setTipsHintAdapter(hintAdapter);
//        searchView.setAutoCompleteAdapter(autoCompleteAdapter);
        //   noView = contentView.findViewById(R.id.popup_wgxx_noview);//关闭弹框
        filterReset = (TextView) contentView.findViewById(R.id.filter_reset_wgxx);//重置
        filterSure = (TextView) contentView.findViewById(R.id.filter_sure_wgxx);//确定
//        noView.setOnClickListener(new WgXXPopWindow.CancelOnClickListener());


        // 重置的点击监听，将所有选项全设为false
        filterReset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                wgxx_name.setText("");
                //;searchView.getEtInput().setText("");
                ///searchView.getIvDelete().setVisibility(View.GONE);
               // wgxx_search_grid.setWgName("");
            }
        });
        // 确定的点击监听，将所有已选中项列出
        filterSure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String wgName = wgxx_name.getText().toString().trim();
                Message msg = new Message();
                Bundle data = new Bundle();
                data.putString("wgName", wgName);
                msg.setData(data);
                msg.what = 1;
                handler.sendMessage(msg);
                dismiss();
            }
        });

        // step_wg_search.setVisibility(View.VISIBLE);

        this.setContentView(contentView);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        ColorDrawable dw = new ColorDrawable(00000000);
        this.setBackgroundDrawable(dw);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        this.update();

    }




    public class CancelOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    }

    public boolean onKeyDown(Context context, int keyCode, KeyEvent event) {
        this.context = context;
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return true;
    }

    public void showFilterPopup(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


}
